﻿using System;
using System.Configuration;
using System.Linq;
using Controllers;

namespace Web
{
    public partial class MeetingRoomEdit : System.Web.UI.Page
    {
        protected ReqMeetingRoom reqMeetingRoom;
        protected string empName;
        ApolloOaDataContext ctx = new ApolloOaDataContext(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string formNo = Request.QueryString["formNo"];
                if (formNo != null)
                {
                    reqMeetingRoom = ctx.ReqMeetingRooms.Single(r => r.FormNo == formNo);
                    Employee employee = ctx.Employees.Single(r => r.Id == reqMeetingRoom.EmpId);
                    empName = employee.Name;
                    PreStartTime.Value = reqMeetingRoom.PreStartTime.ToString("yyyy-MM-dd HH:mm:ss");
                    PreEndTime.Value = reqMeetingRoom.PreEndTime.ToString("yyyy-MM-dd HH:mm:ss");
                    RoomName.Value = reqMeetingRoom.RoomName;
                    IsNeedProjector.Value = reqMeetingRoom.IsNeedProjector.ToString();
                    Cause.Value = reqMeetingRoom.Cause;
                }
                else
                {
                    EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
                    empName = ebi.Name;
                    reqMeetingRoom = new ReqMeetingRoom();
                    reqMeetingRoom.ReqDate = DateTime.Now;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string formNo = Request.QueryString["formNo"];
            DateTime PreStartTime2 = DateTime.Parse(PreStartTime.Value);
            DateTime PreEndTime2 = DateTime.Parse(PreEndTime.Value);
            DateTime standardTime = new DateTime(PreStartTime2.Year, PreStartTime2.Month, PreStartTime2.Day, 14, 0, 0);

            IQueryable<ReqMeetingRoom> reqMeetingRooms = _hasConflict(PreStartTime2, PreEndTime2, RoomName.Value);
            if (reqMeetingRooms.Any())
            {
                string formNos = "";
                foreach (ReqMeetingRoom r in reqMeetingRooms)
                {
                    if(!string.IsNullOrEmpty(formNo)&&formNo==r.FormNo)
                    {
                        continue;
                    }
                    formNos += ";" + r.FormNo + ":" + r.PreStartTime.ToString("HH:mm") + "~" + r.PreEndTime.ToString("HH:mm");
                }
                Response.Write("<script>alert('" + string.Format("您申请的 会议室时间段 和如下已申请表单({0})有冲突,请更改表单或电话协商解决.", formNos.Trim(';')) + "');window.close();</script>");
                Response.End();
                return;
            }
            EmpBasicInfo ebi = Session["ebi"] as EmpBasicInfo;
            if (!string.IsNullOrEmpty(formNo) && formNo != "undefined")
            {
                reqMeetingRoom = ctx.ReqMeetingRooms.Single(r => r.FormNo == formNo);
            }
            else
            {
                reqMeetingRoom = new ReqMeetingRoom();
                string maxFormNo = ctx.ReqMeetingRooms.Max(r => r.FormNo);
                formNo = "Form_Meet_000001";
                if (!string.IsNullOrEmpty(maxFormNo))
                {
                    formNo = "Form_Meet_" + (int.Parse(maxFormNo.Substring(10)) + 1000001).ToString().Substring(1);
                }
                reqMeetingRoom.FormNo = formNo;
                reqMeetingRoom.ReqDate = DateTime.Now;
                reqMeetingRoom.EmpId = ebi.Id;
                ctx.ReqMeetingRooms.InsertOnSubmit(reqMeetingRoom);
            }
            reqMeetingRoom.PreStartTime = PreStartTime2;
            reqMeetingRoom.PreEndTime = PreEndTime2;
            reqMeetingRoom.RoomName = RoomName.Value;
            reqMeetingRoom.IsNeedProjector = int.Parse(IsNeedProjector.Value);
            reqMeetingRoom.Cause = Cause.Value;
            reqMeetingRoom.StatusId = 0;
            ctx.SubmitChanges();
            Response.Write("<script>alert('修改成功');window.returnValue='" + reqMeetingRoom.FormNo + "';window.close();</script>");
        }

        private IQueryable<ReqMeetingRoom> _hasConflict(DateTime sTime, DateTime eTime, string roomName)
        {
            IQueryable<ReqMeetingRoom> list = from r in ctx.ReqMeetingRooms
                                              where r.RoomName == roomName && (sTime<r.PreEndTime && eTime>r.PreStartTime)
                                              select r;
            return list;
        }
    }
}